
import DecorationControllerInterface from "../../public/interface/DecorationControllerInterface";
import ModuleManager, { ModuleID } from "../../cfw/module/ModuleManager";
import { UIIndex, ItemState } from "../../cfw/tools/Define";
import LivingRoomManager from "./LivingRoomManager";
import LobbyController from "../lobby/LobbyController";
import DecorationColumnModel from "./model/DecorationColumnModel";
import GlobalEvent from "../../cfw/event/GlobalEvent";
import { EventName, SceneName, GameText } from "../../public/Config";
import DecorationItemModel from "./model/DecorationItemModel";
import BaseController from "../../cfw/mvc/BaseController";
import ItemController from "../item/ItemController";
import SnowManController from "../snowman/SnowManController";
import BagManager from "../bag/BagManager";
import { ResultState, SDKEventName } from "../../sdk/SDKConfig";
import TipController from "../tip/TipController";
import SDKManager from "../../sdk/SDKManager";
export default class HouseController extends BaseController implements DecorationControllerInterface, ItemController {

    private static ins: HouseController;

    static instance() {
        if (!this.ins) {
            this.ins = new HouseController()
        }
        return this.ins;
    }

    private gotoLobby: boolean = false;

    isGotoLobby(){
        return this.gotoLobby;
    }

    intoSnowRoom() {
        this.gotoLobby = false
        SnowManController.instance().intoLayer()
    }

    back() {
        this.intoLayer()
    }
    intoLayer() {

        ModuleManager.setModuleID(ModuleID.OUT_DOOR)
        this.pushView('prefabs/OutDoorView', 'OutDoorView', null, ModuleManager.getLoader(), UIIndex.ROOT)
       

    }

    intoHouse() {
        ModuleManager.setModuleID(ModuleID.OUT_DOOR)
        this.gotoLobby = false
        this.pushView('prefabs/HouseRoomView', 'DressingRoomView', new LivingRoomManager(2), ModuleManager.getLoader(), UIIndex.ROOT)
        SDKManager.getChannel().sendEvent(SDKEventName.enterhouse)
    }
    intoLobby() {
        this.gotoLobby = true;
        LobbyController.instance().intoLayer()
    }
    protected tempColumn: DecorationColumnModel;
    selectColumn(model: DecorationColumnModel) {
        if (this.tempColumn != model) {
            this.tempColumn = model;
            GlobalEvent.instance().emit(EventName.SELECT_COLUMN, model.getID())
        }

    }
    protected tempClothe: DecorationItemModel;
    onButtonIconClick(model: DecorationItemModel) {
        if (!model.isOpen()) {
            TipController.instance().showToast(GameText.ITEM_TOAST, { num: model.getOpenLevel() })
            return;
        }
        if (model.needBuy()) {
            let cost: number[] = model.getCostItem();
            if (BagManager.isEnough(cost[0], cost[1])) {
                BagManager.costItem(cost[0], cost[1], (s) => {
                    if (s == ResultState.YES) {
                        model.setState(ItemState.GOT)
                    }
                })
            }
        } else {
            this.useItem(model)
        }
    }


    useItem(model) {
        if (this.tempClothe != model) {
            this.tempClothe = model;
            model.use()
            // GlobalEvent.instance().emit(EventName.SELECT_CLOTHE, model)
        } else {
            if (this.tempClothe.canDel()) {
                this.tempClothe.unUse();
                this.tempClothe = null;
            }


        }
    }
}
